load("//bazel:mongo_src_rules.bzl", "idl_generator", "mongo_cc_library", "mongo_cc_unit_test")

package(default_visibility = ["//visibility:public"])

exports_files(
    glob([
        "*.h",
        "*.cpp",
    ]),
)

idl_generator(
    name = "sharding_api_d_params_gen",
    src = "sharding_api_d_params.idl",
)

idl_generator(
    name = "shard_retry_server_parameters_gen",
    src = "shard_retry_server_parameters.idl",
)

idl_generator(
    name = "sharding_runtime_d_params_gen",
    src = "sharding_runtime_d_params.idl",
)

idl_generator(
    name = "mongos_server_parameters_gen",
    src = "mongos_server_parameters.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "sharding_config_server_parameters_gen",
    src = "sharding_config_server_parameters.idl",
)

idl_generator(
    name = "cluster_commands_gen",
    src = "cluster_commands.idl",
    deps = [
        ":sharding_types_gen",
        "//src/mongo/db:basic_types_gen",
        "//src/mongo/idl:generic_argument_gen",
    ],
)

idl_generator(
    name = "mongod_and_mongos_server_parameters_gen",
    src = "mongod_and_mongos_server_parameters.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "sharding_feature_flags_gen",
    src = "sharding_feature_flags.idl",
)

idl_generator(
    name = "sharding_types_gen",
    src = "sharding_types.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

mongo_cc_library(
    name = "sharding_runtime_d_params_idl",
    srcs = [
        ":sharding_runtime_d_params_gen",
    ],
    deps = [
        "//src/mongo:base",
        "//src/mongo/db/commands:test_commands_enabled",
        "//src/mongo/s:common_s",
        "//src/mongo/util:processinfo",
    ],
)

mongo_cc_library(
    name = "shard_shared_state_cache",
    srcs = [
        "shard_shared_state_cache.cpp",
        ":shard_retry_server_parameters_gen",
    ],
    deps = [
        "//src/mongo/client:retry_strategy",
        "//src/mongo/db:server_base",
    ],
)

mongo_cc_library(
    name = "unittest_noop_sharding_initialization_mongod",
    srcs = [
        "sharding_initialization_mongod_for_test.cpp",
    ],
    deps = [
        "//src/mongo/db/repl:replica_set_aware_service",
    ],
)

mongo_cc_library(
    name = "mongos_server_parameters",
    srcs = [
        "mongos_server_parameters.cpp",
        ":mongos_server_parameters_gen",
    ],
    deps = [
        "//src/mongo/db:server_base",
    ],
)

# Functionality for initializing all the services used by sharding, which are
# common between MongoS and MongoD
mongo_cc_library(
    name = "sharding_initialization",
    srcs = [
        "sharding_initialization.cpp",
        "//src/mongo/db/router_role:routing_table_cache_gossip_metadata_hook.cpp",
        "//src/mongo/db/sharding_environment/client:sharding_connection_hook.cpp",
        "//src/mongo/db/sharding_environment/client:sharding_network_connection_hook.cpp",
        "//src/mongo/s:client_metadata_propagation_egress_hook.cpp",
        "//src/mongo/s:initialize_tenant_to_shard_cache.cpp",
        "//src/mongo/s:sharding_task_executor_pool_controller.cpp",
        "//src/mongo/s:sharding_task_executor_pool_gen",
    ],
    deps = [
        "//src/mongo/db:server_base",
        "//src/mongo/db:server_feature_flags",
        "//src/mongo/db/global_catalog:sharding_catalog_client_impl",
        "//src/mongo/db/repl:repl_coordinator_interface",
        "//src/mongo/executor:async_multicaster",
        "//src/mongo/executor:connection_pool_executor",
        "//src/mongo/executor:scoped_task_executor",
        "//src/mongo/executor:thread_pool_task_executor",
        "//src/mongo/rpc:client_metadata",
        "//src/mongo/rpc:metadata",
        "//src/mongo/rpc:metadata_impersonated_user",
        "//src/mongo/s:grid",
        "//src/mongo/s:query_analysis_sampler",
        "//src/mongo/s:sharding_task_executor",
        "//src/mongo/util:periodic_runner_factory",
    ],
)

mongo_cc_library(
    name = "sharding_test_fixture_common",
    srcs = [
        "sharding_test_fixture_common.cpp",
        "//src/mongo/db/router_role/routing_cache:catalog_cache_loader_mock.cpp",
        "//src/mongo/db/router_role/routing_cache:catalog_cache_mock.cpp",
        "//src/mongo/db/router_role/routing_cache:config_server_catalog_cache_loader_mock.cpp",
        "//src/mongo/db/router_role/routing_cache:shard_server_catalog_cache_loader_mock.cpp",
    ],
    deps = [
        "//src/mongo/client:remote_command_targeter_mock",
        "//src/mongo/db:service_context_test_fixture",
        "//src/mongo/db/shard_role:resource_yielders",
        "//src/mongo/db/shard_role/lock_manager",
        "//src/mongo/db/transaction",
        "//src/mongo/executor:network_interface_mock",
        "//src/mongo/executor:network_test_env",
        "//src/mongo/executor:thread_pool_task_executor_test_fixture",
        "//src/mongo/s:grid",
        "//src/mongo/s:sharding_router_api",
        "//src/mongo/s/write_ops:batch_write_types",
    ],
)

mongo_cc_library(
    name = "sharding_mongos_test_fixture",
    srcs = [
        "sharding_mongos_test_fixture.cpp",
        "//src/mongo/db/router_role/routing_cache:catalog_cache_test_fixture.cpp",
    ],
    deps = [
        ":sharding_initialization",
        ":sharding_test_fixture_common",
        "//src/mongo/db/global_catalog:sharding_catalog_client_impl",
        "//src/mongo/db/query/collation:collator_factory_mock",
        "//src/mongo/db/transaction",
        "//src/mongo/executor:task_executor_pool",
        "//src/mongo/s:sharding_router_api",
        "//src/mongo/s:sharding_task_executor",
        "//src/mongo/transport:transport_layer_mock",
        "//src/mongo/util:clock_source_mock",
    ],
)

mongo_cc_library(
    name = "sharding_logging",
    srcs = [
        "sharding_logging.cpp",
    ],
    deps = [
        "//src/mongo/db:shard_role_api",
        "//src/mongo/db/global_catalog:sharding_catalog_client_impl",
    ],
)

mongo_cc_library(
    name = "sharding_mongod_test_fixture",
    srcs = [
        "sharding_mongod_test_fixture.cpp",
    ],
    deps = [
        ":sharding_test_fixture_common",
        "//src/mongo/db:service_context_d_test_fixture",
        "//src/mongo/db/op_observer",
        "//src/mongo/db/op_observer:op_observer_impl",
        "//src/mongo/db/op_observer:operation_logger_impl",
        "//src/mongo/db/repl:replmocks",
        "//src/mongo/db/repl:storage_interface_impl",
        "//src/mongo/db/s:sharding_runtime_d",
        "//src/mongo/db/session:session_catalog_mongod",
    ],
)

mongo_cc_library(
    name = "shard_server_test_fixture",
    srcs = [
        "shard_server_test_fixture.cpp",
    ],
    deps = [
        ":sharding_mongod_test_fixture",
        "//src/mongo/db/global_catalog:sharding_catalog_client_impl",
    ],
)

mongo_cc_library(
    name = "config_server_test_fixture",
    srcs = [
        "config_server_test_fixture.cpp",
    ],
    deps = [
        ":sharding_mongod_test_fixture",
        "//src/mongo/db/global_catalog:sharding_catalog_client_impl",
        "//src/mongo/db/op_observer",
        "//src/mongo/db/op_observer:op_observer_impl",
        "//src/mongo/db/op_observer:operation_logger_impl",
    ],
)

mongo_cc_unit_test(
    name = "shard_server_op_observer_test",
    srcs = [
        "shard_server_op_observer_test.cpp",
    ],
    tags = ["mongo_unittest_first_group"],
    deps = [
        "//src/mongo/db:service_context_d_test_fixture",
        "//src/mongo/db/auth:authmocks",
        "//src/mongo/db/op_observer:op_observer_util",
        "//src/mongo/db/repl:replmocks",
        "//src/mongo/db/repl:storage_interface_impl",
        "//src/mongo/db/s:sharding_runtime_d",
    ],
)
